package com.itheima.controller;
import lombok.extern.slf4j.Slf4j;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;
//    @GetMapping
//    public Result page(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer pageSize){
//        PageBean pageBean = empService.page(page, pageSize);
//        return Result.success(pageBean);
//    }
    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询，参数：{}",empQueryParam);
        PageBean pageBean = empService.page(empQueryParam);
        return Result.success(pageBean);
    }
    @PostMapping //添加员工
    public Result add(@RequestBody Emp emp){
        log.info("添加员工信息：{}",emp);
        empService.add(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有员工信息");
        List<Emp> list = empService.findAll();
        return Result.success(list);
    }
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工信息：{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工信息：{}",id);
        Emp emp = empService.getInfoById(id);
        return Result.success(emp);
    }
//    @PostMapping
//    public Result update(@RequestBody Emp emp){
//        log.info("修改员工信息：{}",emp);
//        empService.update(emp);
//        return Result.success();
//    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工信息：{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
